<?php namespace Phpcmf\Controllers;

/**
 * 二次开发时可以修改本文件，不影响升级覆盖
 */

class Bisai extends \Phpcmf\Home\Mform
{

    public function index() {
        //$this->_Home_Show();
		    
		    $id = intval(\Phpcmf\Service::L('input')->get('id'));
		    $name = 'module_'.MOD_DIR.'_form_'.$this->form['table'].'_show_id_'.$id;
		    $cache = \Phpcmf\Service::L('cache')->get_data($name);
			
		    if (!$cache) {
		        list($tpl, $data) = $this->_Show($id);
		        if (!$data) {
		            $this->_msg(0, dr_lang('表单内容【id#%s】不存在', $id));
		        }
		        // 获取父级内容
		        $this->cid = intval($data['cid']);
		        $this->index = $this->_Module_Row($this->cid);
		        if (!$this->index) {
		            $this->_msg(0, dr_lang('模块内容【id#%s】不存在',  $this->cid));
		        }
		        // 模块的处理
		        $data = $this->_Call_Show($data);
		        $cache = [
		            $tpl,
		            $data,
		            $this->cid,
		            $this->index,
		        ];
				$tieziuid = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_form_son')->where('id', $data['tid'])->get()->getRowArray();
		        // 缓存结果
		        if ($data['uid'] != $this->uid && SYS_CACHE) {
		            if ($this->member && $this->member['is_admin']) {
		                // 管理员时不进行缓存
		                \Phpcmf\Service::L('cache')->init()->delete($name);
		            } else {
		                \Phpcmf\Service::L('cache')->set_data($name, $cache, SYS_CACHE_SHOW * 3600);
		            }
		        }
		    } else {
		        list($tpl, $data, $this->cid, $this->index) = $cache;
		    }
			//判断是否有阅读权
			if($tieziuid['chakanjieguo'] == '0'){
				
			} elseif ($tieziuid['chakanjieguo'] == '1'){
				if($data['uid'] == $this->uid || $tieziuid['uid'] == $this->uid){
					
				} else {
					$this->_msg(0, dr_lang('只有作者 和 参赛者 可阅读'));
				}
				
			} elseif ($tieziuid['chakanjieguo'] == '3') {
				
			}
			//判断是否收费的帖子
			if ($data['vip'] == 1) {
				//判断是否付费进圈
				if ($this->index['price'] > 0) {
					if ($this->index['uid'] != $this->uid) {
						//判断是否购买
						$goumai = \Phpcmf\Service::M()->table('1_'.MOD_DIR.'_buy')->where('uid', $this->uid)->where('cid', $this->index['id'])->getRow();
						$gouurl = dr_url(MOD_DIR.'/buy/index',['fid'=>0,'id'=>$this->index['id']]);
						if (!$goumai) {
							$this->_msg(0, dr_lang('请付费'.$this->index['price'].'元 进圈<a href="'.$gouurl.'">【立即付费】</\a> 进入 '.$this->index['title']));
						}
						if ($goumai['endtime'] < time()) {
							$this->_msg(0, dr_lang('请续费'.$this->index['price'].'元 进圈<a href="'.$gouurl.'">【立即付费】</\a> 进入 '.$this->index['title']));
						}
					}
				}
			}
		
		    // 无权限访问表单
		    if ($this->form['setting']['is_read'] && !in_array($this->uid, [$data['uid'], $this->index['uid']])) {
		        $this->_msg(0, dr_lang('无权限访问表单'));
		    }
		
		    if ($data['status'] != 1) {
		        $this->_msg(0, dr_lang('内容正在审核中'));
		    }
		
		    $data['cat'] = [];
		    if (isset($data['catid']) && $data['catid']) {
		        $data['cat'] = dr_cat_value($this->module['mid'], $data['catid']);
		    }
		
		    \Phpcmf\Service::V()->assign($data);
		
		    $data['cat'] = [];
		    $data['catname'] = $data['catpname'] = '';
		    if (isset($data['catid']) && $data['catid']) {
		        $data['cat'] = dr_cat_value($this->module['mid'], $data['catid']);
		        $data['catname'] = $data['cat']['name'];
		        $data['catpname'] = dr_get_cat_pname($this->module, $data['catid'], SITE_SEOJOIN);
		    } else {
		
		    }
			
		
		    $data['formname'] = dr_lang($this->form['name']);
		    $data['modulename'] = $data['modname'] = dr_lang($this->module['name']);
			//奖励
			//$jianglis = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', $this->module['name'])->get()->getRowArray();
			$jianglis = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', $this->form['table'])->get()->getRowArray();
			$jiangli = dr_string2array($jianglis['setting']);
			//统计次数经验值
			$tongjijingyanzhi = \Phpcmf\Service::M()->db->table('member_explog')->where('tieid', $id)->countAllResults();
			//统计次数tongjijifen
			$tongjijifen = \Phpcmf\Service::M()->db->table('member_scorelog')->where('tieid', $id)->where('uid', $this->uid)->countAllResults();
			if($jiangli['yueducishu']){
				$cishu = $jiangli['yueducishu']-$tongjijifen;
			}
			
			$yuedujiangli = [
				'jingyan' => intval($jiangli['yueduexp']),
				'jifen' => intval($jiangli['yueduscore']),
				'yue' => intval($jiangli['yuedumoney']),
				'cishu' => $cishu,
				'miaoshu' => intval($jiangli['yuedujimiao']),
				
			];
			//进度
			if ($data['jindu'] == 1) {
				$xuqiu = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 1)->where('cid', $data['id'])->get()->getRowArray();
				$paiqi = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 2)->where('cid', $data['id'])->get()->getRowArray();
				$kaifa = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 3)->where('cid', $data['id'])->get()->getRowArray();
				$wancheng = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 4)->where('cid', $data['id'])->get()->getRowArray();
			}
			$jindus = [
				'xuqiu' => $xuqiu,
				'paiqi' => $paiqi,
				'kaifa' => $kaifa,
				'wancheng' => $wancheng,
			];
			//BUG
			if ($data['bug'] == 1) {
				$xuqiu = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 1)->where('cid', $data['id'])->get()->getRowArray();
				$paiqi = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 2)->where('cid', $data['id'])->get()->getRowArray();
				$kaifa = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 3)->where('cid', $data['id'])->get()->getRowArray();
				$wancheng = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 4)->where('cid', $data['id'])->get()->getRowArray();
			}
			$bugs = [
				'xuqiu' => $xuqiu,
				'paiqi' => $paiqi,
				'kaifa' => $kaifa,
				'wancheng' => $wancheng,
			];
			
			
		
		    // seo
		    \Phpcmf\Service::V()->assign(\Phpcmf\Service::L('Seo')->get_seo_value($data, [
		        'meta_title' => isset($this->form['setting']['seo']['title']) && $this->form['setting']['seo']['title'] ? $this->form['setting']['seo']['title'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		        'meta_keywords' => isset($this->form['setting']['seo']['keywords']) && $this->form['setting']['seo']['keywords'] ? $this->form['setting']['seo']['keywords'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		        'meta_description' => isset($this->form['setting']['seo']['description']) && $this->form['setting']['seo']['description'] ? $this->form['setting']['seo']['description'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
		    ]));
		
		    \Phpcmf\Service::V()->assign([
		        'index' => $this->index,
		        'catid' => intval($this->index['catid']),
		        'markid' => 'module-'.MOD_DIR.'-'.intval($this->index['catid']),
		        'urlrule' =>\Phpcmf\Service::L('Router')->mform_show_url($this->form['table'], $this->index['id'], MOD_DIR, '[page]'),
				'name' => $this->module['name'],
				'jiangli' => dr_string2array($yuedujiangli),
				'jindus' => dr_string2array($jindus),
				'bugs' => dr_string2array($bugs),
		    ]);
		    \Phpcmf\Service::V()->display("mform_show-bisai-show.html");
		
    }

    public function show() {
		
        $this->_Home_Show();
    }

    public function post() {
        $this->_Home_Post();
    }
	//比赛积分奖励
	public function jianglijifen() {
	    //$id = (int)\Phpcmf\Service::L('input')->get('id');
		$leixing = (int)\Phpcmf\Service::L('input')->get('id');
		$this->_module_init($mid);
		$rt = \Phpcmf\Service::M('bisai', MOD_DIR)->jianglijifen(SITE_ID.'_'.MOD_DIR.$mid, $mid,$leixing);
	    $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	//比赛作品审核
	public function daanshenhe() {
	    //$id = (int)\Phpcmf\Service::L('input')->get('id');
		$leixing = (int)\Phpcmf\Service::L('input')->get('id');
		$this->_module_init($mid);
		$rt = \Phpcmf\Service::M('bisai', MOD_DIR)->daanshenhe(SITE_ID.'_'.MOD_DIR.$mid, $mid,$leixing);
	    $this->_json($rt['code'], $rt['msg'], $rt['data']);
	}
	
}
